package com.wj.infrastructure.mapper;

import com.wj.domain.vo.ChapterSimpleVO;
import com.wj.domain.vo.ChapterVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @description: 
 * @author: wj
 * @create: 2021/11/30 19:54
 */
@Mapper
public interface ChapterMapper{

    @Select("select count(id) from chapter where book_id=#{bookId}")
    int countChapter(@Param("bookId") Long bookId);

    @Select("SELECT c.id, c.book_id as bookId, c.chapter_title as chapterTitle, " +
            " c.content_id as contentId," +
            " c.create_time as createTime, c.sort, c.chapter_url as chapterUrl" +
            " FROM chapter AS c where c.book_id=#{bookId} order by c.sort asc")
    List<ChapterVO> queryListByBookId(@Param("bookId") Long bookId);

    @Select("SELECT c.id, c.book_id as bookId, c.chapter_title as chapterTitle, " +
            " c.content_id as contentId," +
            " c.create_time as createTime, c.sort, c.chapter_url as chapterUrl" +
            " FROM chapter AS c where c.book_id=#{bookId} and c.id > #{chapterId} order by c.sort asc")
    List<ChapterVO> queryListByBookIdAndFromChapterId(@Param("bookId") Long bookId, @Param("chapterId") Long chapterId);

    @Select(" select c.sort from chapter AS c " +
            " where book_id=#{bookId} order by c.sort asc limit 1 ")
    int getFirstSort(@Param("bookId") Long bookId);

    @Select("<script>" +
            " select c.id, c.book_id as bookId, c.chapter_title as chapterTitle, " +
            " c.content_id as contentId," +
            " c.create_time as createTime, c.sort, c.chapter_url as chapterUrl " +
            " from chapter as c where c.book_id=#{bookId} and c.sort = #{sort} " +
            "</script>")
    ChapterVO getChapterInfo(@Param("bookId") Long bookId, @Param("sort") int sort);

    @Select("<script>" +
            " select c.id as chapterId, c.chapter_title as chapterTitle, " +
            " c.book_id as bookId, c.sort " +
            " from chapter as c where c.book_id=#{bookId} and c.sort &lt; #{sort} " +
            " order by c.sort desc limit 1" +
            "</script>")
    ChapterSimpleVO getPreChapter(@Param("bookId") Long bookId, @Param("sort") int sort);

    @Select("<script>" +
            " select c.id as chapterId, c.chapter_title as chapterTitle, " +
            " c.book_id as bookId, c.sort " +
            " from chapter as c where c.book_id=#{bookId} and c.sort &gt; #{sort} " +
            " order by c.sort asc limit 1" +
            "</script>")
    ChapterSimpleVO getNextChapter(@Param("bookId") Long bookId, @Param("sort") int sort);

}
